package com.cya.lotterysystem.common.interceptor;

import com.cya.lotterysystem.common.utils.JWTUtil;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * @author ：陈奕安（3048279304@qq.com）
 * @date ：Created in 2025/10/31 16:40
 * @description：拦截除登录界面所有的管理界面
 * @modified By：
 * @version:
 */
//定义拦截器
@Component
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {



    /**
     * 预处理，业务请求之前调用
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("user_token");
        log.info("获取token:{}",token);
        log.info("获取路径：{}",request.getRequestURI());
//        令牌解析
        Claims claims= JWTUtil.parseJWT(token);
        if (claims==null){
            log.error("解析令牌失效");
            response.setStatus(401);
            return false;
        }
        log.info("解析JWT成功！：允许操作");
        return true;

    }
}